﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.ComponentModel;
using serviceMaker.UserManagementService;
using serviceMaker.RssService;
using System.IO.IsolatedStorage;

namespace ViewModel
{
    public sealed class ClientInformation 
    {
        private static volatile ClientInformation instance;
        private static Object syncRoot = new Object();

        public CookieContainer cc
        { get; set; }
        
        public static ClientInformation Instance
        {
            get
            {
                if (instance == null)
                    lock (syncRoot)
                        if (instance == null)
                            instance = new ClientInformation();
                return instance;
            }

        }

        private DataUser currentUser;
        public DataUser CurrentUser
        {
            get
			{
				return currentUser;
			}
            set
            { 
                if ( value != null && currentUser != value)
                    currentUser = value;
            }
        }


        public bool Authenticate
        { get; set; }

        private ClientInformation()
        {
            currentUser = new DataUser
            {
                name = "",
                email = "",
                password = ""
            };
            Authenticate = false;
            cc = getCookie();
        }

        public CookieContainer getCookie()
        {

            try
            {
                var storage = IsolatedStorageSettings.ApplicationSettings;
                if (storage != null && storage["CookieContainer"] != null)
                    return storage["CookieContainer"] as CookieContainer;
                else
                    return new CookieContainer();
            }
            catch 
            {
                return new CookieContainer();
            }
        }

        public void SaveCookie(CookieContainer cookie)
        {
            try
            {
                var storage = IsolatedStorageSettings.ApplicationSettings;
                if (storage != null)
                {
                    storage.Remove("CookieContainer");
                    storage.Add("CookieContainer", cookie);
                }
            }
            catch
            {
                return;
            }
        }

        public Guid userRootDirectory
        { get; set; }

    }
}
